import { ref } from 'vue'
import {
  addressDelByIds,
  addressSave,
  addressUpdateStateByIds,
  getAddressDetail,
  getAddressList,
} from '@/api/return-factory'

/**
 * 地址表单服务
 */
export const useAddressForm = () => {
  // 表单保存加载
  const formSaveLoading = ref(false)

  // 表单
  const form = ref<ReturnFactoryAPIRequest.AddressSave>({
    address: '',
    username: '',
    status: false,
    province: '',
    city: '',
    id: '',
    phone: '',
    area: '',
    defaultAddr: false,
  })

  /**
   * 表单保存事件
   */
  const formSave = async () => {
    formSaveLoading.value = true
    const res = await addressSave(form.value)
    formSaveLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      address: '',
      username: '',
      status: false,
      province: '',
      city: '',
      id: '',
      phone: '',
      area: '',
      defaultAddr: false,
    }
  }

  return {
    form,
    formSaveLoading,
    formSave,
    resetForm,
  }
}

/**
 * 虎丘地址列表
 */
export const useGetAddressList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref({
    keyword: '',
    limit: 50,
    page: 1,
  })

  // 列表
  const list = ref<Array<ReturnFactoryAddress>>([])

  // 列表总条数
  const listTotal = ref(0)

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await getAddressList(getListParam.value)
    list.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
    getListLoading.value = false
  }

  return {
    list,
    listTotal,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 批量删除地址
 */
export const useAddressDelByIds = () => {
  // 删除加载
  const delLoading = ref(false)

  /**
   * 删除
   * @param ids - 地址id集合
   */
  const del = async (ids: Array<string>) => {
    delLoading.value = true
    const res = await addressDelByIds(ids)
    delLoading.value = false
    return res.data
  }

  return {
    delLoading,
    del,
  }
}

/**
 * 批量启用地址
 */
export const useAddressEnableByIds = () => {
  // 启用加载
  const enableLoading = ref(false)

  /**
   * 启用
   * @param ids - 地址id集合
   */
  const enable = async (ids: Array<string>) => {
    enableLoading.value = true
    const res = await addressUpdateStateByIds(ids, false)
    enableLoading.value = false
    return res.data
  }

  return {
    enableLoading,
    enable,
  }
}

/**
 * 批量禁用地址
 */
export const useAddressDisableByIds = () => {
  // 禁用加载
  const disableLoading = ref(false)

  /**
   * 禁用
   * @param ids - 地址id集合
   */
  const disable = async (ids: Array<string>) => {
    disableLoading.value = true
    const res = await addressUpdateStateByIds(ids, true)
    disableLoading.value = false
    return res.data
  }

  return {
    disableLoading,
    disable,
  }
}

/**
 * 获取地址详情
 */
export const useGetAddressDetail = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 详情
  const detail = ref<ReturnFactoryAddressDetail>({
    status: false,
    username: '',
    defaultAddr: false,
    province: '',
    city: '',
    area: '',
    address: '',
    phone: '',
    id: '',
  })

  /**
   * 获取详情
   * @param id - 地址id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getAddressDetail(id)
    detail.value = res.data.data.row
    getDetailLoading.value = false
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}
